<?php
class PublicAction extends Action
{
	/*
	private $uname;

  public _initialize()
	{
			$this->uname = Session::get('username');
			if($this->uname == 'admin')
			{
					Session::set('admin','admin');		
			}
	}
 */
	public function login()
    {
    	$this->display('public:login');
    }
    
    //checklogin
	public function checklogin()
	{	
		$username = isset($_POST["username"]) ? $_POST["username"] : Cookie::get('username');
		$password = isset($_POST["password"]) ? md5($_POST["password"]) :Cookie::get('pass');
	
		if(!empty($username) && !empty($password))
		{
			$u = D('ybkguest');
			$map['gname'] = $username;
			$map['password'] = $password;
			//默认激活为0
			$map['genable'] = 1;

			$user = $u->where($map)->find();
			//dump($user);
			//exit;
			if($user)
			{
				//保存当前的状态
				Session::set('uid', $user['gid']);
				Session::set('username',$user['gname']);
				//dump($user['gadmin']); exit;
			  if($user['gadmin'] == '1')
				{
					  //设置管理员SESSION
						Session::set('aid',$user['gid']);
						//Session::set('username',$user['gname']);
						//

				}

				//这里要不要独立出来对用户的行为进行跟踪呢？
				//预留接口 loguser();
				
				//保存用户COOKIE
		
				unset($username,$password);
				//判断原来的URL在跳转回去，如果原来为空跳转到用户首页
				$this->redirect('index/index');
			} else {
				//
				//dump('error1');
				$this->redirect('public/login');
			}
		} else {
				//输出错误信息
				//dump('error');
				$this->redirect('public/login');
		}
	}

	public function logout()
	{
		Session::destroy();
		//header('Location: '.__APP__);
		$this->redirect('public/login');
	}
}
?>